package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.SubjectOrderAmount;
import com.atguigu.edu.publisher.beans.SubjectOrderCt;
import com.atguigu.edu.publisher.beans.SubjectUserCt;
import com.atguigu.edu.publisher.service.SubjectStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//localhost:8070/subjectOrderAmount?date=20221021
@RestController
public class SubjectStatsController {
    @Autowired
    private SubjectStatsService subjectStatsService;

    @RequestMapping("/subjectOrderAmount")
    public String getSubjectOrderAmount(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<SubjectOrderAmount> subjectOrderAmountList = subjectStatsService.getOrderAmount(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder orderAmountValues = new StringBuilder("[");

        for (int i = 0; i < subjectOrderAmountList.size(); i++) {
            SubjectOrderAmount subjectOrderAmount = subjectOrderAmountList.get(i);
            String subjectName = subjectOrderAmount.getSubjectName();
            Double orderAmount = subjectOrderAmount.getOrderAmount();

            categories.append("\"").append(subjectName).append("\"");
            orderAmountValues.append("\"").append(orderAmount).append("\"");

            if (i < subjectOrderAmountList.size() - 1) {
                categories.append(",");
                orderAmountValues.append(",");
            } else {
                categories.append("]");
                orderAmountValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"下单金额\",\n" +
                "        \"data\": " + orderAmountValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/subjectOrderCt")
    public String getSubjectOrderCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<SubjectOrderCt> orderCtList = subjectStatsService.getOrderCt(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder orderCtValues = new StringBuilder("[");

        for (int i = 0; i < orderCtList.size(); i++) {
            SubjectOrderCt subjectOrderCt = orderCtList.get(i);
            String subjectName = subjectOrderCt.getSubjectName();
            Integer orderCt = subjectOrderCt.getOrderCt();

            categories.append("\"").append(subjectName).append("\"");
            orderCtValues.append("\"").append(orderCt).append("\"");

            if (i < orderCtList.size() - 1) {
                categories.append(",");
                orderCtValues.append(",");
            } else {
                categories.append("]");
                orderCtValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"下单次数\",\n" +
                "        \"data\": " + orderCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }

    @RequestMapping("/subjectUserCt")
    public String getSubjectUserCt(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateFormatUtil.now();
        }
        List<SubjectUserCt> userCtList = subjectStatsService.getUserCt(date);
        StringBuilder categories = new StringBuilder("[");
        StringBuilder orderUserCtValues = new StringBuilder("[");

        for (int i = 0; i < userCtList.size(); i++) {
            SubjectUserCt subjectUserCt = userCtList.get(i);
            String subjectName = subjectUserCt.getSubjectName();
            Integer userCt = subjectUserCt.getUserCt();

            categories.append("\"").append(subjectName).append("\"");
            orderUserCtValues.append("\"").append(userCt).append("\"");

            if (i < userCtList.size() - 1) {
                categories.append(",");
                orderUserCtValues.append(",");
            } else {
                categories.append("]");
                orderUserCtValues.append("]");
            }
        }
        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\":" + categories + ",\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"下单人数\",\n" +
                "        \"data\": " + orderUserCtValues + "\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";
    }
}
